package com.joesem.sale.client.logic.workOrder.option

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jiaxing.sale.client.R
import com.jiaxing.sale.client.databinding.DialogAreaBinding
import com.joesem.sale.client.base.BaseBottomFragmentDialog
import com.joesem.sale.client.base.createViewModel
import com.joesem.sale.client.base.gone
import com.joesem.sale.client.base.visible
import com.joesem.sale.client.logic.workOrder.option.viewModel.AreaViewModel
import com.joesem.sale.client.network.data.AreaMode
import com.joesem.sale.client.utils.click


class AreaSelectedDialog : BaseBottomFragmentDialog<DialogAreaBinding>() {
    companion object {
        var selectedResult: (List<AreaMode>) -> Unit =
            {

            }
    }

    private val adapter = object : BaseQuickAdapter<AreaMode, BaseViewHolder>(R.layout.item_area) {
        override fun convert(holder: BaseViewHolder, item: AreaMode) {
            holder.setText(R.id.tvName, item.name)
            holder.itemView.click {
                addTab(item)
                if (item.hasChildren == true) {
                    refresh(item.id)
                } else {
                    selectedResult()
                }
            }
        }

    }
    private val viewMode by createViewModel<AreaViewModel>()

    override fun DialogAreaBinding.initView(savedInstanceState: Bundle?) {
        rvAreaContent.adapter = adapter
        bind.tbSelected.addTab(bind.tbSelected.newTab().apply {
            tag = "0"
            text = "请选择"
        })
    }

    override fun DialogAreaBinding.initListener() {
        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onCreate(owner: LifecycleOwner) {
                refresh()
            }
        })
    }

    private fun refresh(parentId: Int? = null) {
        bind.llLoading.visible()
        bind.rvAreaContent.gone()
        viewMode.getArea(parentId)
    }

    override fun DialogAreaBinding.initIntent(intent: Any?) {
        when (intent) {
            is AreaViewModel.AreaIntent -> {
                bind.llLoading.gone()
                bind.rvAreaContent.visible()
                adapter.setList(intent.data)
            }
        }
    }

    override fun contentMax(): Int {
        val height = resources.displayMetrics.heightPixels
        return height - (height / 3)
    }

    private fun addTab(item: AreaMode?) {
        if (bind.tbSelected.tabCount > 0) {
            bind.tbSelected.getTabAt(bind.tbSelected.tabCount - 1)?.apply {
                tag = item
                view.click {
                    this.text = "请选择"
                    if (tag != null) {
                        val currentMode = tag as AreaMode
                        refresh(currentMode.pid)
                    }
                    tag = null
                    for (i in position + 1 until bind.tbSelected.tabCount) {
                        bind.tbSelected.removeTabAt(bind.tbSelected.tabCount - 1)
                    }

                }
            }?.text = item?.name.orEmpty()
        }
        if (item?.hasChildren == true) {
            bind.tbSelected.addTab(bind.tbSelected.newTab().apply {
                text = "请选择"
            })
            bind.tbSelected.postDelayed({
                bind.tbSelected.getTabAt(bind.tbSelected.tabCount - 1)?.select()
            }, 400)
        }

    }

    private fun selectedResult() {
        val address = mutableListOf<AreaMode>()
        for (i in 0 until bind.tbSelected.tabCount) {
            val tag = bind.tbSelected.getTabAt(i)?.tag
            if (tag is AreaMode) {
                address.add(tag)
            }
        }
        selectedResult.invoke(address)
        dismiss()
    }
}

fun optionArea(
    fm: FragmentManager,
    selectedResult: (province: AreaMode?, city: AreaMode?, county: AreaMode?) -> Unit
) {
    AreaSelectedDialog().apply {
        AreaSelectedDialog.selectedResult = {
            var province: AreaMode? = null
            var city: AreaMode? = null
            var county: AreaMode? = null
            it.forEachIndexed { index, areaMode ->
                when (index) {
                    0 -> province = areaMode
                    1 -> city = areaMode
                    2 -> county = areaMode
                }
            }
            selectedResult.invoke(province, city, county)

        }
    }.show(fm, null)
}